﻿<%@ WebHandler Language="C#" Class="SYS_User_add" %>

using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Web.SessionState;

public class SYS_User_add : IHttpHandler,IRequiresSessionState {
    sqlhelp sql = new sqlhelp();
    result result = new result();
    string str = "";
    public void ProcessRequest (HttpContext context) {
        try
        {
            if (context.Session["comid"] == null)
            {
                result.code = "-1";
                result.msg = "用户未登录";
                unit.send(context, result);
                return;
            }
            
            string Id = "";
            string updateStr = "";

            string Tel = "";
            if (context.Request["Tel"] != null)
            {
                Tel = context.Request["Tel"].ToString();
            }
            else
            {
                result.code = "0";
                result.msg = "未传递用户手机号参数";
                unit.send(context, result);
                return;
            }
            
            string StoreId = context.Session["comid"].ToString();

            System.Data.DataTable dtTel = sql.myDataSet("select Id,StoreId from SYS_User where Tel = '" + Tel + "'").Tables[0];
            if (dtTel.Rows.Count == 0) {
                result.code = "0";
                result.msg = "不存在该用户";
                unit.send(context, result);
                return;
            }

            if (dtTel.Rows[0]["StoreId"].ToString() != "" && dtTel.Rows[0]["StoreId"].ToString() != "00000000-0000-0000-0000-000000000000")
            {
                result.code = "0";
                result.msg = "该用户已经被拉入公司";
                unit.send(context, result);
                return;
            }
            Id = dtTel.Rows[0]["Id"].ToString();
            
            updateStr = updateStr + " StoreId = '" + StoreId + "',";
            
            updateStr = updateStr.TrimEnd(',');

            str = "update SYS_User set " + updateStr + " where Id = '" + Id + "'"; ;
            
            if (sql.ExecuteNonQuery(str) > 0)
            {
                result.code = "1";
                result.msg = "";
            }
            else
            {
                result.code = "0";
                result.msg = "修改失败";
            }

        }
        catch (Exception e1)
        {
            result.code = "0";
            result.msg = e1.Message;
        }
        unit.send(context, result);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
